//
//  MyCollectionViewController.m
//  Founder_study
//
//  Created by hongbao.cui on 14-8-21.
//  Copyright (c) 2014年 com.founder. All rights reserved.
//

#import "MyCollectionViewController.h"
#import "Sqlite.h"
#import "ApplicationUtils.h"
#import "MyCollectionExerciseViewController.h"
#import "FMNavgationItem.h"
@interface MyCollectionViewController ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>{
    NSMutableArray *array;
}
@property(nonatomic,strong) NSMutableArray *array;
@property(nonatomic,strong)UITableView *tableView;
;

@end

@implementation MyCollectionViewController

@synthesize array;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)initNavigationBar{
    self.navigationItem.title = @"我的收藏";
}
-(void)initSelfData{
    
    //    NSArray *titleArray = [NSArray arrayWithObjects:@"清空收藏记录",@"清空做题记录",@"版本升级",@"用户反馈",@"关于",@"注销帐户", nil];
    //    NSArray *subTitleArray = [NSArray arrayWithObjects:@"清空所有的收藏试题",@"清空试卷的做题状态",@"版检查软件的最新版本",@"欢迎反馈您宝贵的意见",@"建筑工业出版社版权所有",@"注销当前登陆帐户", nil];
    if (!self.array) {
        self.array = [NSMutableArray arrayWithCapacity:1];
    }
    [self.array removeAllObjects];
    NSMutableArray *sys = [Sqlite selectTestCollectionQuestion];
    [self.array addObjectsFromArray:sys];
}
-(void)initTableView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, FMSCREENWIDTH, [UIScreen mainScreen].bounds.size.height-20-44) style:UITableViewStylePlain];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    [self.view addSubview:_tableView];
}
-(void)back{
    [self.navigationController dismissViewControllerAnimated:YES completion:^{
        
    }];
}
-(void)initBack{
    FMNavgationItem* leftItem = [[FMNavgationItem alloc] init];
    [leftItem.backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItems = [leftItem getNavgationItemWithType:WRNavgationTypeLeft Ima:[ApplicationUtils getLocalImage:@"Navigation-Bar_Return_btn_@2x"] ImaHighlight:[ApplicationUtils getLocalImage:@"Navigation-Bar_Return_btn_@2x"] title:@""];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self initNavigationBar];
    [self initTableView];
    [self initBack];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    if (IS_STUDY) {
//        
//    }else{
//        self.navigationController.navigationBarHidden = NO;
//    }
    [self initSelfData];
    [_tableView reloadData];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
//    if (IS_STUDY) {
//        
//    }else{
//        self.navigationController.navigationBarHidden = YES;
//    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)dealloc{
    self.array = nil;
}
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)newsize{
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    UIGraphicsBeginImageContext(newsize);
    // 绘制改变大小的图片
    [img drawInRect:CGRectMake(0, 0, newsize.width, newsize.height)];
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    // 返回新的改变大小后的图片
    return scaledImage;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    if ([self.array count]) {
        return [self.array count];
    }else{
        return 1;
    }
}
-(void)selectCatergoryBtnClicked:(id)sender{
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    NSInteger row = indexPath.row;
    NSInteger  section = indexPath.section;
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCell"] ;
    }
    if ([self.array count]) {
        cell.indentationLevel = 2;
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 10, 250, 15)];
        NSString *titleStr = [[self.array objectAtIndex:row] objectForKey:@"paper_title"];
        [titleLabel setText:titleStr];
        [titleLabel setFont:[UIFont systemFontOfSize:14.0]];
        [cell.contentView addSubview:titleLabel];

        NSString *paper_id = [[self.array objectAtIndex:row] objectForKey:@"paper_id"];
        //        NSString *qtype = [[self.array objectAtIndex:row] objectForKey:@"qtype"];
        NSInteger count = [Sqlite queryCollectionCount:titleStr paper_Id:paper_id];
        NSString *countStr = [NSString stringWithFormat:@"数量:  %d",count];
        UILabel *subTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 25, 150, 15)];
        [subTitleLabel setText:countStr];
        [subTitleLabel setFont:[UIFont systemFontOfSize:12.0]];
        [cell.contentView addSubview:subTitleLabel];
        
        UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, 50)];
        [bgImageView setImage:[ApplicationUtils getLocalImage:@"selector_lib_item_bg"]];
        cell.selectedBackgroundView = bgImageView;

        cell.imageView.image =[ApplicationUtils getLocalImage:@"fav_icon"] ;//[self scaleToSize:[ApplicationUtils getLocalImage:@"fav_icon"] size:CGSizeMake(21, 20)];
        [tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    }else{
        cell.textLabel.text = @"暂无数据";
        cell.textLabel.font = [UIFont systemFontOfSize:20.0];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.imageView.image = nil;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50.0;
}

// Override to support conditional editing of the table view.
// - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
// {
// // Return NO if you do not want the specified item to be editable.
//     return YES;
// }
//

/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
 {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
 }
 else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }
 }
 */

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
 {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
 {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.array count] == 0) {
        return;
    }
    MyCollectionExerciseViewController *sysnExerciseViewController2 = [[MyCollectionExerciseViewController alloc] initWithNibName:nil bundle:nil];
    NSInteger row = indexPath.row;
    NSString *titleStr = [[self.array objectAtIndex:row] objectForKey:@"paper_title"];
    NSString *paper_id = [[self.array objectAtIndex:row] objectForKey:@"paper_id"];
    //    NSString *qtype = [[self.array objectAtIndex:row] objectForKey:@"qtype"];
    NSInteger count = [Sqlite queryCollectionCount:titleStr paper_Id:paper_id];
    NSDictionary *dict_ = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:[self.array objectAtIndex:row]];
    [dict addEntriesFromDictionary:dict_];
    sysnExerciseViewController2.inforDict = dict;
    [self.navigationController pushViewController:sysnExerciseViewController2 animated:YES];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
